<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The Collator class</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="intl.examples.basic.html">Basic usage of this extension</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="collator.asort.html">Collator::asort</a></div>
 <div class="up"><a href="book.intl.html">intl</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="class.collator" class="reference">
 <h1 class="title">The Collator class</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.3.0, PECL intl &gt;= 1.0.0)</p>

  
  <div class="section" id="collator.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    Provides string comparison capability with support for appropriate
    locale-sensitive sort orderings.
   </p>
  </div>
  

  <div class="section" id="collator.synopsis">
   <h2 class="title">类摘要</h2>

   
   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    
    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">Collator</strong>
     </span>
     {</div>
    

    

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="collator.construct.html" class="methodname">__construct</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$locale</code></span>
   )</div>

    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">bool</span>
   <span class="methodname"><a href="collator.asort.html" class="methodname">asort</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$arr</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sort_flag</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">int</span>
   <span class="methodname"><a href="collator.compare.html" class="methodname">compare</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str1</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$str2</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="modifier">static</span>
   <span class="type">Collator</span>
   <span class="methodname"><a href="collator.create.html" class="methodname">create</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$locale</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">int</span>
   <span class="methodname"><a href="collator.getattribute.html" class="methodname">getAttribute</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$attr</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">int</span>
   <span class="methodname"><a href="collator.geterrorcode.html" class="methodname">getErrorCode</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">string</span>
   <span class="methodname"><a href="collator.geterrormessage.html" class="methodname">getErrorMessage</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">string</span>
   <span class="methodname"><a href="collator.getlocale.html" class="methodname">getLocale</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">string</span>
   <span class="methodname"><a href="collator.getsortkey.html" class="methodname">getSortKey</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">int</span>
   <span class="methodname"><a href="collator.getstrength.html" class="methodname">getStrength</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">bool</span>
   <span class="methodname"><a href="collator.setattribute.html" class="methodname">setAttribute</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$attr</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$val</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">bool</span>
   <span class="methodname"><a href="collator.setstrength.html" class="methodname">setStrength</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$strength</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">bool</span>
   <span class="methodname"><a href="collator.sortwithsortkeys.html" class="methodname">sortWithSortKeys</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$arr</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type">bool</span>
   <span class="methodname"><a href="collator.sort.html" class="methodname">sort</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$arr</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sort_flag</code></span>
  ] )</div>

   }</div>
   

  </div>

  

  

<div class="section" id="intl.collator-constants">
 <h2 class="title">预定义常量</h2>
 <p class="para">
  <dl>

   
    <dt id="collator.constants.french-collation">
     <strong><code>Collator::FRENCH_COLLATION</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      Sort strings with different accents from the back of the string. This
      attribute is automatically set to
      <em class="emphasis">On</em>
      for the French locales and a few others. Users normally would not need
      to explicitly set this attribute. There is a string comparison
      performance cost when it is set <em class="emphasis">On</em>,
      but sort key length is unaffected. Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::ON</code></strong></li>
       <li class="member"><strong><code>Collator::OFF</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
     <p class="para">
      <div class="example" id="example-2419">
       <p><strong>Example #1 FRENCH_COLLATION rules</strong></p>
       <div class="example-contents"><p>
        <ul class="simplelist">
         <li class="member">F=OFF cote &lt; coté &lt; côte &lt; côté </li>
         <li class="member">F=ON cote &lt; côte &lt; coté &lt; côté</li>
        </ul>
       </p></div>
      </div>
     </p>
    </dd>

   

   
    <dt id="collator.constants.alternate-handling">
     <strong><code>Collator::ALTERNATE_HANDLING</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      The Alternate attribute is used to control the handling of the so called
      variable characters in the UCA: whitespace, punctuation and symbols. If
      Alternate is set to <em class="emphasis">NonIgnorable</em>
      (N), then differences among these characters are of the same importance
      as differences among letters. If Alternate is set to
      <em class="emphasis">Shifted</em>
      (S), then these characters are of only minor importance. The
      <em class="emphasis">Shifted</em> value is often used in combination with
      <em class="emphasis">Strength</em>
      set to Quaternary. In such a case, whitespace, punctuation, and symbols
      are considered when comparing strings, but only if all other aspects of
      the strings (base letters, accents, and case) are identical. If
      Alternate is not set to Shifted, then there is no difference between a
      Strength of 3 and a Strength of 4. For more information and examples,
      see Variable_Weighting in the
      <a href="http://www.unicode.org/reports/tr10/" class="link external">&raquo;&nbsp;UCA</a>.
      The reason the Alternate values are not simply
      <em class="emphasis">On</em> and <em class="emphasis">Off</em>
      is that additional Alternate values may be added in the future. The UCA
      option Blanked is expressed with Strength set to 3, and Alternate set to
      Shifted. The default for most locales is NonIgnorable. If Shifted is
      selected, it may be slower if there are many strings that are the same
      except for punctuation; sort key length will not be affected unless the
      strength level is also increased.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::NON_IGNORABLE</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::SHIFTED</code></strong></li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
     <p class="para">
      <div class="example" id="example-2420">
       <p><strong>Example #2 ALTERNATE_HANDLING rules</strong></p>
       <div class="example-contents"><p>
        <ul class="simplelist">
         <li class="member">
         S=3, A=N di Silva &lt; Di Silva &lt; diSilva &lt; U.S.A. &lt; USA
         </li> 
         <li class="member">
         S=3, A=S di Silva = diSilva &lt; Di Silva &lt; U.S.A. = USA
         </li> 
         <li class="member">
         S=4, A=S di Silva &lt; diSilva &lt; Di Silva &lt; U.S.A. &lt; USA
         </li>
        </ul>
       </p></div>
      </div>
     </p>
    </dd>

   

   
    <dt id="collator.constants.case-first">
     <strong><code>Collator::CASE_FIRST</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      The Case_First attribute is used to control whether uppercase letters
      come before lowercase letters or vice versa, in the absence of other
      differences in the strings. The possible values are
      <em class="emphasis">Uppercase_First</em>
      (U) and <em class="emphasis">Lowercase_First</em>
      (L), plus the standard <em class="emphasis">Default</em>
      and <em class="emphasis">Off</em>.
      There is almost no difference between the Off and Lowercase_First
      options in terms of results, so typically users will not use
      Lowercase_First: only Off or Uppercase_First. (People interested in the
      detailed differences between X and L should consult the <em>Collation
      Customization</em>). Specifying either L or U won&#039;t affect string comparison
      performance, but will affect the sort key length.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::OFF</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::LOWER_FIRST</code></strong></li>
       <li class="member"><strong><code>Collator::UPPER_FIRST</code></strong></li>
       <li class="member"><strong><code>Collator:DEFAULT</code></strong></li>
      </ul>
     </p>
     <p class="para">
      <div class="example" id="example-2421">
       <p><strong>Example #3 CASE_FIRST rules</strong></p>
       <div class="example-contents"><p>
        <ul class="simplelist">
         <li class="member">C=X or C=L &quot;china&quot; &lt; &quot;China&quot; &lt; &quot;denmark&quot; &lt; &quot;Denmark&quot;</li> 
         <li class="member">C=U &quot;China&quot; &lt; &quot;china&quot; &lt; &quot;Denmark&quot; &lt; &quot;denmark&quot;</li>
        </ul>
       </p></div>
      </div>
     </p>
    </dd>

   

   
    <dt id="collator.constants.case-level">
     <strong><code>Collator::CASE_LEVEL</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      The Case_Level attribute is used when ignoring accents but not case. In
      such a situation, set Strength to be <em class="emphasis">Primary</em>,
      and Case_Level to be <em class="emphasis">On</em>.
      In most locales, this setting is Off by default. There is a small
      string comparison performance and sort key impact if this attribute is
      set to be <em class="emphasis">On</em>.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::OFF</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::ON</code></strong></li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
     <p class="para">
      <div class="example" id="example-2422">
       <p><strong>Example #4 CASE_LEVEL rules</strong></p>
       <div class="example-contents"><p>
        <ul class="simplelist">
         <li class="member">S=1, E=X role = Role = rôle</li> 
         <li class="member">S=1, E=O role = rôle &lt; Role</li>
        </ul>
       </p></div>
      </div>
     </p>
    </dd>

   

   
    <dt id="collator.constants.normalization-mode">
     <strong><code>Collator::NORMALIZATION_MODE</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      The Normalization setting determines whether text is thoroughly
      normalized or not in comparison. Even if the setting is off (which is
      the default for many locales), text as represented in common usage will
      compare correctly (for details, see UTN #5). Only if the accent marks
      are in noncanonical order will there be a problem. If the setting is
      <em class="emphasis">On</em>,
      then the best results are guaranteed for all possible text input.
      There is a medium string comparison performance cost if this attribute
      is <em class="emphasis">On</em>,
      depending on the frequency of sequences that require normalization.
      There is no significant effect on sort key length. If the input text is
      known to be in NFD or NFKD normalization forms, there is no need to
      enable this Normalization option.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::OFF</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::ON</code></strong></li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
    </dd>

   

   
    <dt id="collator.constants.strength">
     <strong><code>Collator::STRENGTH</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      The ICU Collation Service supports many levels of comparison (named
      &quot;Levels&quot;, but also known as &quot;Strengths&quot;). Having these categories
      enables ICU to sort strings precisely according to local conventions.
      However, by allowing the levels to be selectively employed, searching
      for a string in text can be performed with various matching conditions.
      For more detailed information, see
      <span class="function"><a href="collator.setstrength.html" class="function">collator_set_strength()</a></span> chapter.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::PRIMARY</code></strong></li>
       <li class="member"><strong><code>Collator::SECONDARY</code></strong></li>
       <li class="member"><strong><code>Collator::TERTIARY</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::QUATERNARY</code></strong></li>
       <li class="member"><strong><code>Collator::IDENTICAL</code></strong></li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
    </dd>

   

   
    <dt id="collator.constants.hiragana-quaternary-mode">
     <strong><code>Collator::HIRAGANA_QUATERNARY_MODE</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      Compatibility with JIS x 4061 requires the introduction of an additional
      level to distinguish Hiragana and Katakana characters. If compatibility
      with that standard is required, then this attribute should be set
      <em class="emphasis">On</em>,
      and the strength set to Quaternary. This will affect sort key length
      and string comparison string comparison performance.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::OFF</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::ON</code></strong></li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
    </dd>

   

   
    <dt id="collator.constants.numeric-collation">
     <strong><code>Collator::NUMERIC_COLLATION</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <p class="para">
      When turned on, this attribute generates a collation key for the numeric
      value of substrings of digits. This is a way to get &#039;100&#039; to sort AFTER
      &#039;2&#039;.
     </p>
     <p class="para">
      Possible values are:
      <ul class="simplelist">
       <li class="member"><strong><code>Collator::OFF</code></strong>(default)</li>
       <li class="member"><strong><code>Collator::ON</code></strong></li>
       <li class="member"><strong><code>Collator::DEFAULT_VALUE</code></strong></li>
      </ul>
     </p>
    </dd>

   

   
    <dt id="collator.constants.default-value">
     <strong><code>Collator::DEFAULT_VALUE</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.primary">
     <strong><code>Collator::PRIMARY</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.secondary">
     <strong><code>Collator::SECONDARY</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.tertiary">
     <strong><code>Collator::TERTIARY</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.default-strength">
     <strong><code>Collator::DEFAULT_STRENGTH</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.quaternary">
     <strong><code>Collator::QUATERNARY</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.identical">
     <strong><code>Collator::IDENTICAL</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.off">
     <strong><code>Collator::OFF</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.on">
     <strong><code>Collator::ON</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.shifted">
     <strong><code>Collator::SHIFTED</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.non-ignorable">
     <strong><code>Collator::NON_IGNORABLE</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.lower-first">
     <strong><code>Collator::LOWER_FIRST</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   

   
    <dt id="collator.constants.upper-first">
     <strong><code>Collator::UPPER_FIRST</code></strong>
     (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)
    </dt>

    <dd>

     <span class="simpara">

     </span>
    </dd>

   
  </dl>

 </p>
</div>

 </div>

 





















































































<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="collator.asort.html">Collator::asort</a> — Sort array maintaining index association</li><li><a href="collator.compare.html">Collator::compare</a> — Compare two Unicode strings</li><li><a href="collator.construct.html">Collator::__construct</a> — Create a collator</li><li><a href="collator.create.html">Collator::create</a> — Create a collator</li><li><a href="collator.getattribute.html">Collator::getAttribute</a> — Get collation attribute value</li><li><a href="collator.geterrorcode.html">Collator::getErrorCode</a> — Get collator's last error code</li><li><a href="collator.geterrormessage.html">Collator::getErrorMessage</a> — Get text for collator's last error code</li><li><a href="collator.getlocale.html">Collator::getLocale</a> — Get the locale name of the collator</li><li><a href="collator.getsortkey.html">Collator::getSortKey</a> — Get sorting key for a string</li><li><a href="collator.getstrength.html">Collator::getStrength</a> — Get current collation strength</li><li><a href="collator.setattribute.html">Collator::setAttribute</a> — Set collation attribute</li><li><a href="collator.setstrength.html">Collator::setStrength</a> — Set collation strength</li><li><a href="collator.sortwithsortkeys.html">Collator::sortWithSortKeys</a> — Sort array using specified collator and sort keys</li><li><a href="collator.sort.html">Collator::sort</a> — Sort array using specified collator</li></ul>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="intl.examples.basic.html">Basic usage of this extension</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="collator.asort.html">Collator::asort</a></div>
 <div class="up"><a href="book.intl.html">intl</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
